package com.start.manager.controller;


import com.start.manager.domain.entity.Company;
import com.start.manager.domain.vo.CompanyVO;
import com.start.manager.service.ICompanyService;
import com.start.manager.util.PageUtil;
import com.start.manager.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lijian
 * @since 2019-12-29
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private ICompanyService companyService;

    /**
     * 查询分页获取
     * @param companyVO
     * @return
     */
    @GetMapping(value = "/getList")
    @ResponseBody
    public ResponseResult getList(@ModelAttribute CompanyVO companyVO){
        PageUtil pageUtil =  companyService.findList(companyVO);
        return ResponseResult.success(pageUtil);
    }

    /**
     * 新增和更新
     * @param companyVO
     * @return
     */
    @PostMapping(value="/saveCompany")
    @ResponseBody
    public ResponseResult saveCompany(@RequestBody CompanyVO companyVO) {
        Company byId = null;
        if (StringUtils.isNotBlank(companyVO.getId())){
            byId = companyService.getById(companyVO.getId());
        }
        if (byId == null){
            boolean result = companyService.saveCompany(companyVO);
            if (!result){
                return ResponseResult.error("添加失败");
            }
            return ResponseResult.success("添加成功");
        }else{
            //更新用户操作
            BeanUtils.copyProperties(companyVO,byId);
            boolean result = companyService.updateById(byId);
            if (!result){
                return ResponseResult.error("更新失败");
            }
            return ResponseResult.success("更新成功");
        }

    }

    /**
     * 删除
     * @param companyId
     * @return
     */
    @DeleteMapping(value="/deleteCompany/{companyId}")
    @ResponseBody
    public ResponseResult deleteCompany( @PathVariable String companyId){
        boolean result = companyService.removeById(companyId);
        if (!result){
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }

}
